package Controller

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"hr-system/Global"
	"hr-system/Model"
	"hr-system/Response"
	"strconv"
	"time"
)

type IDepartmentManageController interface {
	RestController
	Update(c *gin.Context)
	Delete(c *gin.Context)
}

type DepartmentManageController struct {
	DB *gorm.DB
}

func NewDepartmentManageController() IDepartmentManageController{
	db := Global.GetDB()
	return DepartmentManageController{DB: db}
}

func (d DepartmentManageController) PageList(c *gin.Context) {
	var requestD Model.Department
	_ = c.ShouldBind(&requestD)
	name := requestD.DepartmentName
	if name != "" {
		var selectDepartment Model.Department

		// 查询
		err := d.DB.Where("department_name=?", name).Find(&selectDepartment).Error
		if err != nil {
			response.Fail(c, err.Error(), nil)
			return
		}
		re := make([]Model.RequestDepartment, 0)
		re = append(re, Model.RequestDepartment{
			UniDepartment: selectDepartment.UniDepartment,
			IncorporationDate: selectDepartment.IncorporationDate.Format("2006-01-02"),
		})
		response.Success(c, gin.H{"data": re}, "查询成功")
		return
	}

	var dpm []Model.Department
	d.DB.Order("department_number").Find(&dpm)
	length := len(dpm)
	request := make([]Model.RequestDepartment,0)
	for i := 0; i < length; i++ {
		t := dpm[i].IncorporationDate
		if name != "" && (name != dpm[i].DepartmentName) {
			continue
		}
		request = append(request, Model.RequestDepartment{
			UniDepartment:   dpm[i].UniDepartment,
			IncorporationDate:  t.Format("2006-01-02"),
		})
	}
	response.Success(c, gin.H{"data": request}, "查询成功")
}

func (d DepartmentManageController) Create(c *gin.Context) {
	var requestDepartment Model.RequestDepartment
    _ = c.ShouldBind(&requestDepartment)
    result := d.DB.Model(&Model.Department{}).Where("department_number=?",requestDepartment.DepartmentNumber).First(&Model.Department{}).RowsAffected
    if result != 0 {
    	response.Fail(c, "部门号已存在", nil)
    	return
    }
    r := d.DB.Where("id=?",requestDepartment.DepartmentHead).First(&Model.Employee{}).RowsAffected
    if r == 0 {
		response.Fail(c, "此员工不存在", nil)
		return
	}
    reDepartment := Model.Department{
    	UniDepartment: requestDepartment.UniDepartment,
	}
	reDepartment.IncorporationDate,_ = time.Parse("2006-01-02",requestDepartment.IncorporationDate)
    err := d.DB.Create(&reDepartment).Error
    if err != nil {
    	response.Fail(c, err.Error(), nil)
    	return
    }
    response.Success(c, gin.H{"data": requestDepartment} , "创建成功")
}

func (d DepartmentManageController) Update(c *gin.Context) {
	// 绑定body中参数（更新后的值）
    var requestDepartment Model.RequestDepartment
    _ = c.ShouldBind(&requestDepartment)
    id := requestDepartment.DepartmentNumber
    // 更新
	reDepartment := Model.Department{
		UniDepartment: requestDepartment.UniDepartment,
	}
	reDepartment.IncorporationDate,_ = time.Parse("2006-01-02",requestDepartment.IncorporationDate)
    err := d.DB.Model(&Model.Department{}).Where("department_number=?", id).Updates(&reDepartment).Error
    if err != nil {
    	response.Fail(c, err.Error(), nil)
    	return
    }
    response.Success(c, gin.H{"data": requestDepartment}, "修改成功")
}

func (d DepartmentManageController) Delete(c *gin.Context) {
	idStr, _ := c.GetPostForm("department_number")
	id, _ := strconv.Atoi(idStr)
    // 查询
	result := d.DB.Model(&Model.Department{}).Where("department_number=?",id).First(&Model.Department{}).RowsAffected
	if result == 0 {
		response.Fail(c, "部门不存在", nil)
		return
	}
	// 删除
    err := d.DB.Where("department_number=?", id).Delete(&Model.Department{}).Error
    if err != nil {
    	response.Fail(c, err.Error(), nil)
    	return
    }
    response.Success(c, nil, "删除成功")
}
